package com.yowasa.xiuxian.engine.item;

import cn.hutool.json.JSONUtil;
import com.yowasa.xiuxian.annoations.Item;
import com.yowasa.xiuxian.annoations.SE;
import com.yowasa.xiuxian.entity.bo.DescInterface;
import com.yowasa.xiuxian.entity.bo.item.ItemAttr;
import com.yowasa.xiuxian.entity.bo.item.UserItemBo;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.enums.ItemTypeEnum;
import com.yowasa.xiuxian.entity.enums.QualityEnum;
import org.apache.commons.lang3.tuple.Pair;

import java.text.MessageFormat;

public interface ItemInterface extends DescInterface {
    String desc();

    @Override
    default Pair<String, String> getItemDesc() {
        Item annotation = this.getClass().getAnnotation(Item.class);
        if (annotation == null) {
            return null;
        }
        String name = annotation.name();
        return Pair.of(name, "[道具]" + name + "\n描述:" + desc());
    }

    default String getName() {
        Item annotation = this.getClass().getAnnotation(Item.class);
        if (annotation == null) {
            return null;
        }
        return annotation.name();
    }

    default ItemTypeEnum getType() {
        Item annotation = this.getClass().getAnnotation(Item.class);
        if (annotation == null) {
            return null;
        }
        return annotation.type();
    }

    default int getPrice() {
        Item annotation = this.getClass().getAnnotation(Item.class);
        if (annotation == null) {
            return 5;
        }
        return annotation.price();
    }

    String effect(UserBO userBO);

    default String removeEffect(UserBO userBO){
        return "";
    }

    default String attr(QualityEnum qualityEnum){
        return "";
    };

    default UserBO updateByEquip(UserBO userBO, ItemAttr itemAttr){
        userBO.getUserBattle().setHp(userBO.getUserBattle().getHp() + itemAttr.getHp());
        userBO.getUserBattle().setMp(userBO.getUserBattle().getMp() + itemAttr.getMp());
        userBO.getUserBattle().setAtk(userBO.getUserBattle().getAtk() + itemAttr.getAtk());
        userBO.getUserBattle().setSpell(userBO.getUserBattle().getSpell() + itemAttr.getSpell());
        userBO.getUserBattle().setDef(userBO.getUserBattle().getDef() + itemAttr.getDef());
        userBO.getUserBattle().setRes(userBO.getUserBattle().getRes() + itemAttr.getRes());
        userBO.getUserBattle().setSpd(userBO.getUserBattle().getSpd() + itemAttr.getSpd());
        userBO.getUserBattle().setImm(userBO.getUserBattle().getImm() + itemAttr.getImm());
        return userBO;
    }

    default String getAttr(QualityEnum qualityEnum,String attrJson){
        double rate = qualityEnum.getRate();
        ItemAttr itemAttr = JSONUtil.toBean(attrJson,ItemAttr.class);
        itemAttr.setQuality(qualityEnum.getId());
        itemAttr.updateByRate(itemAttr,rate);
        return JSONUtil.toJsonStr(itemAttr);
    }

    default String effectEquip(UserBO user, UserItemBo myItem){
        updateByEquip(user, JSONUtil.toBean(myItem.getItemAttr(),ItemAttr.class));
        return MessageFormat.format("你使用了[{0}]，属性值得到了提升",myItem.getItemName());
    }
}
